package cn.xyinping.hrm.service.impl;

import cn.xyinping.hrm.domain.Login;
import cn.xyinping.hrm.mapper.LoginMapper;
import cn.xyinping.hrm.service.ILoginService;
import cn.xyinping.hrm.service.IMealService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author xyinping
 * @since 2021-11-22
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {
    @Autowired
    private IMealService mealService;
    @Override
    public Login saveLoginAndMeal(Login login, long mealId) {
        // 校验非空
        if(StringUtils.isEmpty(login.getUsername())){
            throw new RuntimeException("用户名不能为空");
        }
        if(StringUtils.isEmpty(login.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        if(StringUtils.isEmpty(mealId)){
            throw new RuntimeException("套餐Id不能为空");
        }
        // 保存login, 只允许保存：username, password, type
        // 账户启用
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        insert(login);
        // 保存套餐
        Long loginId = login.getId();
        mealService.saveRelationLogin(mealId, loginId);
        return login;
    }
}
